﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Navigation;
using Silvermoon.UI;
using Silvermoon.Controls.Base;
using Silvermoon.Controls;
using Silvermoon.Animations;
using Silvermoon;

namespace ShoppingList.Utils
{
    public static class PageTransitions
    {
        private static readonly ScaleTransform explodeTransform = new ScaleTransform(12f, 12f, 1f);
        private static readonly ScaleTransform minimizedTransform = new ScaleTransform(0.5f, 0.5f, 1f);
        private static readonly VisualState emptyState = new VisualState { Duration = 1 };

        public static readonly NavigationTransition Explode = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.Show,
            BackMode = PageTransitionMode.Hide,

            HideCurrent = emptyState,

            ShowTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, 0, 255, PowerEase.In),
                new ScaleTransition(350, explodeTransform, ScaleTransform.One, PowerEase.QuadraticOut)),

            HideTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, LinearEase.Instance),
                new ScaleTransition(350, null, explodeTransform, PowerEase.In)),

            ShowCurrent = emptyState,
        };

        public static readonly NavigationTransition InOut = new NavigationTransition
        {

            ForwardMode = PageTransitionMode.HideAndShow,
            BackMode = PageTransitionMode.HideAndShow,

            HideCurrent = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, PowerEase.Out) { Delay = 200 },
                new ScaleTransition(350, null, minimizedTransform, PowerEase.QuadraticIn)
                ),

            ShowTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, 0, 255, LinearEase.Instance) { Delay = 150 },
                new ScaleTransition(350, explodeTransform, ScaleTransform.One, PowerEase.Out) { Delay = 150 }),

            HideTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, LinearEase.Instance),
                new ScaleTransition(350, null, explodeTransform, PowerEase.In)),

            ShowCurrent = new VisualState("",
                new IntTransition(Control.OpacityProperty, 150, 0, 255, PowerEase.In),
                new ScaleTransition(350, minimizedTransform, ScaleTransform.One, PowerEase.QuadraticOut))
        };


        public static readonly NavigationTransition ZoomExplode = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.Show,
            BackMode = PageTransitionMode.Hide,

            HideCurrent = emptyState,

            ShowTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 200, 0, 255, PowerEase.Out),
                new ScaleTransition(350, ScaleTransform.Zero2D, ScaleTransform.One, PowerEase.QuadraticOut)),

            HideTarget = new VisualState("",
                new IntTransition(Control.OpacityProperty, 350, null, 0, PowerEase.CubicIn),
                new ScaleTransition(350, null, explodeTransform, PowerEase.In)),

            ShowCurrent = emptyState,
        };

        public static readonly NavigationTransition ShiftLeft = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.HideAndShow,
            BackMode = PageTransitionMode.ShowAndHide,

            HideCurrent = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, null, -1f, PowerEase.Out)),
            ShowTarget = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, 1f, 0f, PowerEase.Out)),

            HideTarget = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, null, 1f, PowerEase.Out)),
            ShowCurrent = new VisualState("", new FloatTransition(FormBase.TranslateXFactorProperty, 350, -1f, 0f, PowerEase.Out)),
        };

        public static readonly NavigationTransition FromTop = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.Show,
            BackMode = PageTransitionMode.Hide,

            HideCurrent = new VisualState("", new IntTransition(Control.OpacityProperty, 300, null, 0)),
            ShowTarget = new VisualState("", new IntTransition(Control.OpacityProperty, 300, 0, 255), new FloatTransition(FormBase.TranslateYFactorProperty, 350, 1f, 0f, PowerEase.Out), new FloatTransition(FormBase.VisibilityProperty, 25, 0f, 1f)),

            HideTarget = new VisualState("", new IntTransition(Control.OpacityProperty, 500, null, 80, LinearEase.Instance), new FloatTransition(FormBase.TranslateYFactorProperty, 500, null, 1f, PowerEase.Out)),
            ShowCurrent = new VisualState("", new IntTransition(Control.OpacityProperty, 400, 250, 255)),
        };

        public static readonly NavigationTransition RotateLeft = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.ShowAndHide,
            BackMode = PageTransitionMode.HideAndShow,

            HideCurrent = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, 2f * -360F, 2f * -360F + 90F, CircleEase.In)),
            //            HideCurrent = new VisualState("", new IntTransition(Control.OpacityProperty, 50, null, 0) { Delay = 220 - 50 }),
            ShowTarget = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, 2f * -360F - 90F, 2f * -360F, CircleEase.Out)),

            HideTarget = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, -360f * 2f, 2f * -360F - 90F, CircleEase.In)),
            ShowCurrent = new VisualState("", new FloatTransition(FormBase.RotateVerticalProperty, 350, -360f * 2f + 90F, 2f * -360F, CircleEase.Out)),
            //ShowCurrent = new VisualState("", new IntTransition(Control.OpacityProperty, 50, 0, 255))
        };

        const int duration = 110;

        public static readonly NavigationTransition DetailsEditFlip = new NavigationTransition
        {
            ForwardMode = PageTransitionMode.HideThenShow,
            BackMode = PageTransitionMode.HideThenShow,

            HideCurrent = new VisualState("",
                new FloatTransition { Property = FormBase.RotateHorizonProperty, To = 90F, EasingFunction = CircleEase.In, Duration = duration }
                ),

            ShowTarget = new VisualState(VState.Shown,
                new FloatTransition { Property = FormBase.VisibilityProperty, From = -0f, To = 1F, EasingFunction = LinearEase.Instance, Duration = duration },
                new IntTransition(FormBase.OpacityProperty, 1, null, 255),
                //new FloatTransition { Property = FormBase.RotateHorizonProperty, From = -90F, To = 0F, EasingFunction = CircleEase.Out, Duration = duration }),
                new FloatTransition { Property = FormBase.RotateHorizonProperty, From = -90F, To = 0F, EasingFunction = BackEase.Out, Duration = duration * 4 }),


            HideTarget = new VisualState(VState.Hidden,
                new FloatTransition { Property = FormBase.RotateHorizonProperty, To = 90F, EasingFunction = CircleEase.In, Duration = duration }
            ),

            ShowCurrent = new VisualState("",
                new FloatTransition { Property = FormBase.VisibilityProperty, From = -0f, To = 1F, EasingFunction = LinearEase.Instance, Duration = duration },
                new IntTransition(FormBase.OpacityProperty, 1, null, 255),
                new FloatTransition { Property = FormBase.RotateHorizonProperty, From = -90F, To = 0F, EasingFunction = BackEase.Out, Duration = duration * 4 })
        };
    }
}
